package com.example.bottomnavigationkotlin.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.bottomnavigationkotlin.R
import com.example.bottomnavigationkotlin.adapter.HomeAdapter
import com.example.bottomnavigationkotlin.model.HomeItem
import com.example.bottomnavigationkotlin.model.FuncItem

class HomeFragment : Fragment() {

    private lateinit var recyclerView: RecyclerView
    private lateinit var adapter: HomeAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_home, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        recyclerView = view.findViewById(R.id.recyclerView)
        recyclerView.layoutManager = GridLayoutManager(context, 2).apply {
            spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
                    return when (adapter.getItemViewType(position)) {
                        0, 1, 2 -> 2 // Banner、FuncGrid、Decision占据整行
                        else -> 1 // Product占据半行
                    }
                }
            }
        }

        val funcItems = listOf(
            FuncItem(R.drawable.ic_func_stock, "股票"),
            FuncItem(R.drawable.ic_func_futures, "商品(期货)"),
            FuncItem(R.drawable.ic_func_data, "数据中心"),
            FuncItem(R.drawable.ic_func_decision, "决策"),
            FuncItem(R.drawable.ic_func_rank, "龙虎榜"),
            FuncItem(R.drawable.ic_func_hot, "同花顺热榜"),
            FuncItem(R.drawable.ic_func_limit, "涨停聚焦"),
            FuncItem(R.drawable.ic_func_advisor, "投顾服务"),
            FuncItem(R.drawable.ic_func_kline, "k线训练营"),
            FuncItem(R.drawable.ic_func_app, "应用商店")
        )

        val items = listOf(
            HomeItem.Banner(listOf(
                R.drawable.banner_demo,
                R.drawable.banner_demo,
                R.drawable.banner_demo
            )),
            HomeItem.FuncGrid(funcItems),
            HomeItem.Decision("决策工具", "AI分时顶底"),
            HomeItem.Product(R.drawable.product_demo, "产品1", "¥99.00"),
            HomeItem.Product(R.drawable.product_demo, "产品2", "¥199.00"),
            HomeItem.Product(R.drawable.product_demo, "产品3", "¥299.00"),
            HomeItem.Product(R.drawable.product_demo, "产品4", "¥399.00")
        )

        adapter = HomeAdapter(items) { item ->
            when (item) {
                is HomeItem.Decision -> {
                    // 处理决策工具点击
                }
                is HomeItem.Product -> {
                    // 跳转到商品详情Fragment
                    val fragment = ProductDetailFragment.newInstance(item.title, item.price, item.imageResId)
                    parentFragmentManager.beginTransaction()
                        .replace(R.id.nav_host_fragment_activity_main, fragment)
                        .addToBackStack(null)
                        .commit()
                }
                else -> {}
            }
        }

        recyclerView.adapter = adapter
    }
}